<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt="logo" />
        <div class="title">猛犸运维系统管理平台</div>
        <i
          id="Collapse"
          :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
          @click="isCollapse = !isCollapse"
        ></i>
      </div>
      <div class="info">
        <!-- </div>
      <div class="info"> -->
        <!-- <el-button type="info" @click="logout">退出</el-button> -->
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link"
            >{{ user.username
            }}<i class="el- icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="chpwd">修改密码</el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <!-- 添加用户 -->
      <el-dialog
        title="修改密码"
        :visible.sync="chpwdDialogVisible"
        @close="resetForm('chpwd')"
      >
        <el-form
          :model="chpwdForm"
          :rules="chpwdRules"
          ref="chpwd"
          label-
          width="100px"
        >
          <el-form-item label="用户名">
            <el-input disabled v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="旧的密码" prop="oldPassword">
            <el-input
              type="password"
              v-model="chpwdForm.oldPassword"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="chpwdForm.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="chpwdForm.checkPass"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="chpwdDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="change_pwd">确 定</el-button>
        </span>
      </el-dialog>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '240px'">
        <el-menu
          router
          default-active="1-101"
          background-color="#123"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isCollapse"
        >
          <el-submenu :index="item.id" v-for="item in menulist" :key="item.id">
            <template slot="title"
              ><i class="el-icon-menu"></i
              ><span>{{ item.name }}</span></template
            >
            <el-menu-item
              :index="c.path"
              v-for="c in item.children"
              :key="c.id"
              >{{ c.name }}</el-menu-item
            >
            <!-- <el-menu-item index="1-2">用户列表</el-menu-item> -->
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.getUserInfo()
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value !== this.chpwdForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      menulist: [],
      isCollapse: false,
      user: {},
      chpwdDialogVisible: false,
      chpwdForm: {
        oldPassword: '',
        password: '',
        checkPass: ''
      },
      chpwdRules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请再次输入确认密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm(name) {
      console.log('closing~~~')
      this.$refs[name].resetFields()
    },
    handleCommand(command) {
      console.log('handleCommand', command)
      if (command === 'logout') {
        this.logout()
      } else if (command === 'chpwd') {
        this.chpwdDialogVisible = true
      }
    },
    async getUserInfo() {
      // 带着token发起请求，后台查询用户数据
      const { data: response } = await this.$http.get('users/whoami/')
      if (response.code) {
        return this.$message.error(response.message)
      }
      this.user = response.user
    },
    change_pwd() {
      const name = 'chpwd'
      this.$refs[name].validate(async (valid) => {
        if (valid) {
          const { data: response } = await this.$http.post(
            `users/${this.user.id}/setpwd/`,
            this.chpwdForm
          )
          if (response.code) {
            return this.$message.error(response.message)
          }
          this.chpwdDialogVisible = false
          this.$message.success('密码修改成功')
          this.logout()
        }
      })
    },
    logout() {
      console.log('退出')
      window.localStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: response } = await this.$http.get('users/menulist/')
      if (response.code) {
        return this.$message.error(response.message)
      }
      console.log(response)
      this.menulist = response // 菜单项数组
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  .logo {
    display: flex;
    img {
      width: 30px;
    }
    .title {
      font-size: 24px;
      margin-left: 5px;
    }
    #Collapse {
      font-size: 30px;
      margin-left: 5px;
    }
  }
}
.el-aside {
  background-color: #112233;
}
.el-menu {
  border-right: none;
}
.el-main {
  background-color: #f0f2f4;
}
</style>
